package com.lefu.es.system;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;

import com.lefu.es.constant.UtilConstants;
import com.lefu.es.entity.UserModel;
import com.lefu.es.service.ExitApplication;
import com.lefu.es.service.UserService;
import com.lefu.es.util.LogUtils;
import com.lefu.es.util.SharedPreferencesUtil;
import com.wellness.scale.R;

import java.util.List;
import java.util.Locale;

/**
 * 初始化加载界面
 *
 * @author Leon
 *         2015-11-21
 */
public class LoadingActivity extends Activity {
    private static final String TAG = LoadingActivity.class.getSimpleName();
    public static boolean isPad = false, isV = true;
    public static LoadingActivity mainActivty = null;
    private UserService uservice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        mainActivty = this;
        LogUtils.d(TAG, "================>");
        //isPad();
        UtilConstants.su = new SharedPreferencesUtil(LoadingActivity.this);
        //UtilConstants.FIRST_INSTALL_BODYHARMBABY_SCALE = (String) UtilConstants.su.readbackUp("lefuconfig", "first_body_harmbaby","");
        UtilConstants.FIRST_INSTALL_BABY_SCALE = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_baby_scale", "");
        UtilConstants.FIRST_INSTALL_BATH_SCALE = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_bath_scale", "");
        UtilConstants.FIRST_INSTALL_BODYFAT_SCALE = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_bodyfat_scale", "");
        UtilConstants.FIRST_INSTALL_KITCHEN_SCALE = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_kitchen_scale", "");
        UtilConstants.FIRST_INSTALL_SETTING_PAGER = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_setting_pager", "");
        UtilConstants.FIRST_INSTALL_DETAIL = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_detail", "");
        UtilConstants.FIRST_INSTALL_DAILOG = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_dailog", "");
        UtilConstants.FIRST_INSTALL_SHARE = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_share", "");
        UtilConstants.FIRST_INSTALL_FITBIT = (String) UtilConstants.su.readbackUp("lefuconfig", "first_install_fitbit", "");

        UtilConstants.FIRST_RECEIVE_BODYFAT_SCALE_KEEP_STAND_WITH_BARE_FEET = (String) UtilConstants.su.readbackUp("lefuconfig", "first_badyfat_scale_keep_stand_with_bare_feet", "");

        uservice = new UserService(this);
        ExitApplication.getInstance().addActivity(this);

		/*秤检测次数为0*/
        UtilConstants.checkScaleTimes = 0;

        UtilConstants.su = new SharedPreferencesUtil(LoadingActivity.this);
        UtilConstants.SELECT_LANGUAGE = 1;

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "=================loading");
        loadData();
    }

    @Override
    protected void onDestroy() {
        if (null != UtilConstants.serveIntent) {
            stopService(UtilConstants.serveIntent);
        }
        System.exit(0);
        super.onDestroy();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        final Intent intent;
        /*加载选择用户*/
        UtilConstants.SELECT_USER = (Integer) UtilConstants.su.readbackUp("lefuconfig", "user", Integer.valueOf(0));
        /**加载用户数据*/
        if (UtilConstants.SELECT_USER == 0) {
            /*加载第一个用户数据*/
            getFirstUser();
        } else {
            try {
                UtilConstants.CURRENT_USER = uservice.find(UtilConstants.SELECT_USER);
                /*如果查询不到用户也默认第一个用户*/
                if (null == UtilConstants.CURRENT_USER) {
                    getFirstUser();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /**判断是否存在用户*/
        if (null == UtilConstants.CURRENT_USER) {
            /**不存在用户跳转到用户添加界面*/
            intent = new Intent(this, UserAddActivity.class);
        } else {
            /*加载称类型*/
            UtilConstants.CURRENT_SCALE = UtilConstants.CURRENT_USER.getScaleType();
            Log.d(TAG, "当前的秤类型：" + UtilConstants.CURRENT_SCALE);
            /*蓝牙类型*/
            String blueTooth_type = (String) UtilConstants.su.readbackUp("lefuconfig", "bluetooth_type" + UtilConstants.CURRENT_USER.getId(), String.class);
            /*蓝牙类型不为空跳到测量主界面，为空跳到检测界面*/
                /*是否重新检测*/
            UtilConstants.su.editSharedPreferences("lefuconfig", "reCheck", false);
            switch (UtilConstants.CURRENT_SCALE) {
                case UtilConstants.BATHROOM_SCALE:
                    /***人体称***/
                    intent = new Intent(this, BodyScaleNewActivity.class);
                    break;
                case UtilConstants.BABY_SCALE:
                    /***婴儿称***/
                    intent = new Intent(this, BabyScaleActivity.class);
                    break;
                /***厨房秤***/
                case UtilConstants.KITCHEN_SCALE:
                    if(isZh()){
                        intent = new Intent(this, KitchenScaleActivity.class);
                    }else {
                        intent = new Intent(this, KitchenScaleActivity1.class);
                    }
                    break;
                default:
                    /***脂肪称***/
                    intent= new Intent(this, BodyFatNewActivity.class);
                    break;
            }
        }
        if (intent != null) {
            intent.putExtra("ItemID", UtilConstants.SELECT_USER);
            startActivityForResult(intent, 99);
           /* new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                }
            },1500);*/
            /*if (UtilConstants.BATHROOM_SCALE.equals(UtilConstants.CURRENT_SCALE)) {
                intent1.setClass(LoadingActivity.this, BodyScaleNewActivity.class);
                Log.e(TAG, "to BathScaleActivity");
            } else if (UtilConstants.BABY_SCALE.equals(UtilConstants.CURRENT_SCALE)) {
                intent1.setClass(LoadingActivity.this, BabyScaleActivity.class);
                Log.e(TAG, "to BabyScaleActivity");
            } else if (UtilConstants.KITCHEN_SCALE.equals(UtilConstants.CURRENT_SCALE)) {
                if (isZh()) {
                    intent1.setClass(LoadingActivity.this, KitchenScaleActivity.class);
                } else {
                    intent1.setClass(LoadingActivity.this, KitchenScaleActivity1.class);
                }
                Log.e(TAG, "to KitchenScaleActivity");
            } else {
                intent1.setClass(LoadingActivity.this, BodyFatNewActivity.class);
                Log.e(TAG, "to BodyFatScaleActivity");
            }
            intent1.putExtra("ItemID", UtilConstants.SELECT_USER);
            startActivityForResult(intent1, 99);*/
        }
    }

    /**
     * 加载第一个用户为当前用户
     */
    private void getFirstUser() {
        try {
            List<UserModel> users = uservice.getAllDatas();
            if (null != users && users.size() > 0) {
                UtilConstants.CURRENT_USER = users.get(0);
                UtilConstants.SELECT_USER = users.get(0).getId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isZh() {
        Locale locale = getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否是pad
     */
    private void isPad() {
        DisplayMetrics dm = new DisplayMetrics();
        dm = getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        float density = dm.density;
        double diagonalPixels = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
        double screenSize = diagonalPixels / (160 * density);
        if (screenSize >= 6) {
            isPad = true;
        }
        if (width > height) {
            isV = false;
        } else {
            isV = true;
        }
    }

}
